#cs ----------------------------------------------------------------------------

	AutoIt Version: 3.3.1.1 (beta)
	Author:         Ghetek

	Script Function:
	Tune up and shut down computer

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <Process.au3>
#include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode", 1)
$settingsitem = TrayCreateMenu("Settings")
$DiskCleanupSettingsitem = TrayCreateItem("Disk Cleanup", $settingsitem)
TrayCreateItem("")
$Tuneupitem = TrayCreateItem("Tune-up & Shutdown")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

While 1
	$msg = TrayGetMsg()
	Select
		Case $msg = 0
			ContinueLoop
		Case $msg = $Tuneupitem
			Shutdown_custom()
		Case $msg = $DiskCleanupSettingsitem
			_RunDOS("cleanmgr.exe /sageset:1111")
		Case $msg = $exititem
			ExitLoop
	EndSelect
WEnd

Exit

Func OnAutoItStart()
	If @Compiled Then
		$aProcs = ProcessList(@ScriptName)
		If IsArray($aProcs) Then
			For $i = 1 To $aProcs[0][0] ; ensure that only a single copy is running
				If @AutoItPID <> $aProcs[$i][1] Then
					ProcessClose($aProcs[$i][1]) ; get rid of any duplicate processes
				EndIf
			Next
		EndIf
	EndIf
EndFunc   ;==>OnAutoItStart

Func StatusDisplay($StatSection, $StatDetail)
	TrayTip("Tuneup and Shutdown", $StatSection & " - " & $StatDetail, 20)
EndFunc   ;==>StatusDisplay

Func Shutdown_custom()
	StatusDisplay("Memory", "Optimizing Memory")
	If ProcessExists("rundll32.exe") Then
		ProcessClose("rundll32.exe")
	EndIf
	RunWait("rundll32.exe advapi32.dll,ProcessIdleTasks") ; optimize memory
	StatusDisplay("Temp", "Clearing Temp Files")
	_RunDOS("cleanmgr /sagerun:1111")
	StatusDisplay("Disk", "Defragmenting Volumes")
	_RunDOS("defrag -f " & @HomeDrive)
	;Shutdown(1 + 4 + 16) ; shutdown + Force + Force if hung
EndFunc   ;==>Shutdown_custom

Func ClamWin()
	$CWexec = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\ClamWin", "Path")
	If FileExists($CWexec & "\clamscan.exe") And FileExists($CWexec & "\freshclam.exe") Then
		RunWait(@ComSpec & ' /c "' & $CWexec & '\clamscan.exe" --quiet --no-summary', $CWexec, @SW_HIDE)
	EndIf
EndFunc   ;==>ClamWin
